package util

import (
	"crypto/md5"
	"encoding/hex"
	"gopkg.in/ini.v1"
	"io/ioutil"
	"os"
)

func GetFileMd5(file string) string {
	f, _ := os.OpenFile(file, os.O_RDONLY, 0777)
	content, _ := ioutil.ReadAll(f)
	m := md5.New()
	m.Write(content)
	v := m.Sum([]byte(""))
	return hex.EncodeToString(v)
}

// LoadIni 读取配置文件，prefix 需要单独处理
func LoadIni(path string, sec string, configs ...string) (map[string]string, error) {
	ini.PrettyFormat = false
	cfg, err := ini.Load(path)
	if err != nil {
		return nil, err
	}
	section := cfg.Section(sec)
	configMap := make(map[string]string, 0)
	for _, config := range configs {
		value := section.Key(config)
		configMap[config] = value.Value()
	}
	return configMap, nil
}

func InArray(target string, str_array []string) bool {
	for _, element := range str_array {
		if target == element {
			return true
		}
	}
	return false
}
